const fs = require('fs')
const path = require('path')
const axios = require('axios')
const cheerio = require('cheerio')

const novelName = '烟雨楼'
const filePath = `./download/${novelName}.txt`;
const host = 'https://www.d5xs.net'
const firstChapter = 'https://www.d5xs.net/du_266953/87292687_1.html'
const homePageUrl = 'https://www.d5xs.net/du_266953/'
const chapterDom = '#list dd'
const chapterStart = 12
const titleDom = '.bookname h1'
const contentDom = '#content'
const nextDom = '.next'
const exclude = [
    '这章没有结束，请点击下一页继续阅读！',
    '本小章还未完，请点击下一页继续阅读后面精彩内容！',
    '小主，这个章节后面还有哦，请点击下一页继续阅读，后面更精彩！',
    '【烟雨新书，请多多支持，同名漫画《烟雨楼》和真人听书已上线，抖音号：或者直接搜索：一夕烟雨，不定时会发布烟雨楼动态或者直播消息。】',
    '喜欢烟雨楼请大家收藏：(www.d5xs.net)烟雨楼第五小说更新速度全网最快。'
]

const getChapterList = () => {
    return new Promise((resolve, reject) => {
        axios.get(homePageUrl).then(res => res.data)
            .then(res => {
                let list = []
                const $ = cheerio.load(res)
                $(chapterDom).each((index, item) => {
                    if (index == chapterStart) {
                        const a = $(item).find('a')
                        list.push({
                            url: host + a.attr('href'),
                            title: a.text()
                        })
                    }
                })
                resolve(list)
            })
    })
}

const getContent = (url, nextPage = false) => {
    return new Promise((resolve, reject) => {
        axios.get(url).then(res => res.data)
            .then(res => {
                const $ = cheerio.load(res)
                let title = nextPage ? '' : ('\n' + $(titleDom).text() + '\n')
                if (!nextPage) {
                    console.log(`获取${title}`)
                }
                let content = []
                $(contentDom).find('p').map((index, item) => {
                    let p = $(item).text().trim()
                    if (!exclude.includes(p)) {
                        content.push(p)
                    }
                })
                let result = title + content.join('\n')
                fs.appendFile(filePath, result, (err) => {
                    if (err) throw err;
                    console.log(`写入${title || '下一页'}`);
                    const nextUrl = host + $(nextDom).attr('href')
                    if (nextUrl) {
                        getContent(nextUrl, $(nextDom).text().indexOf('下一页') > -1)
                    }
                });
            })
    })
}

const bootstrap = async () => {
    getContent(firstChapter)
    // const chapterList = await getChapterList()
    // console.log(chapterList)
    // for (let i = 0; i < chapterList.length; i++) {
    //     let chapter = chapterList[i]
    //     try {
    //         let content = await getContent(chapter)
    //         console.log(content)
    //     } catch(err) {
    //         console.log(err)
    //     }
    // }
}
bootstrap()
